<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="机构名称" prop="institutionId">
        <el-select v-model="queryParams.institutionId"  filterable placeholder="请选择机构" clearable size="small" @change="selectGrade">
          <el-option
            v-for="dept in deptList"
            :key="dept.institutionId"
            :label="dept.institutionName"
            :value="dept.institutionId"
          />
        </el-select>
      </el-form-item>

      <el-form-item label="年级名称" prop="gradeId">
        <el-select v-model="queryParams.gradeId" placeholder="请选择年级" clearable size="small" @change="selectClass">
          <el-option
            v-for="dept in gradeList"
            :key="dept.gradeId"
            :label="dept.gradeName"
            :value="dept.gradeId"
          />
        </el-select>
      </el-form-item>

      <el-form-item label="班级名称" prop="classId">
        <el-select v-model="queryParams.classId" placeholder="请选择班级" clearable size="small" >
          <el-option
            v-for="dept in classList"
            :key="dept.classId"
            :label="dept.className"
            :value="dept.classId"
          />
        </el-select>
      </el-form-item>

      <el-form-item label="孩子名称" prop="childName">
        <el-input
          v-model="queryParams.childName"
          placeholder="请输入孩子名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
<!--
      <el-form-item label="孩子乳名" prop="childInfantname">
        <el-input
          v-model="queryParams.childInfantname"
          placeholder="请输入孩子乳名"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="孩子性别" prop="childSex">
        <el-select v-model="queryParams.childSex" placeholder="请选择孩子性别" clearable size="small">
          <el-option
            v-for="dict in childSexOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="孩子生日" prop="childBirthday">
        <el-date-picker clearable size="small"
          v-model="queryParams.childBirthday"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="选择孩子生日">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="孩子身份证号" prop="childIdcard">
        <el-input
          v-model="queryParams.childIdcard"
          placeholder="请输入孩子身份证号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="孩子头像;对应文件表sys_file中的file_id" prop="childHead">
        <el-input
          v-model="queryParams.childHead"
          placeholder="请输入孩子头像;对应文件表sys_file中的file_id"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="民族" prop="nation">
        <el-input
          v-model="queryParams.nation"
          placeholder="请输入民族"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="第一联系人电话" prop="firscontacts">
        <el-input
          v-model="queryParams.firscontacts"
          placeholder="请输入第一联系人电话"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="第一联系人名称" prop="firscontactsName">
        <el-input
          v-model="queryParams.firscontactsName"
          placeholder="请输入第一联系人名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="第二联系人电话" prop="secondContacts">
        <el-input
          v-model="queryParams.secondContacts"
          placeholder="请输入第二联系人电话"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="第二联系人名称" prop="secondContactsName">
        <el-input
          v-model="queryParams.secondContactsName"
          placeholder="请输入第二联系人名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="入园时间" prop="joinTime">
        <el-date-picker clearable size="small"
          v-model="queryParams.joinTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="选择入园时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="孩子状态:对应字典表sys_dictionary中的Status类型" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择孩子状态:对应字典表sys_dictionary中的Status类型" clearable size="small">
          <el-option label="请选择字典生成" value="" />
        </el-select>
      </el-form-item>
      <el-form-item label="考勤机是否拉去:对应字典表sys_dictionary中的IsNot类型" prop="isUpdate">
        <el-input
          v-model="queryParams.isUpdate"
          placeholder="请输入考勤机是否拉去:对应字典表sys_dictionary中的IsNot类型"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="记录状态:对应字典表sys_dictionary中的RecordStatus类型" prop="recordStatus">
        <el-select v-model="queryParams.recordStatus" placeholder="请选择记录状态:对应字典表sys_dictionary中的RecordStatus类型" clearable size="small">
          <el-option label="请选择字典生成" value="" />
        </el-select>
      </el-form-item>
      <el-form-item label="记录状态:对应字典表sys_dictionary中的RecordStatus类型" prop="createUser">
        <el-input
          v-model="queryParams.createUser"
          placeholder="请输入记录状态:对应字典表sys_dictionary中的RecordStatus类型"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="记录状态:对应字典表sys_dictionary中的RecordStatus类型" prop="updateUser">
        <el-input
          v-model="queryParams.updateUser"
          placeholder="请输入记录状态:对应字典表sys_dictionary中的RecordStatus类型"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>-->
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['tongban:child:add']"
        >新增</el-button>
      </el-col>
<!--      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['tongban:child:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['tongban:child:remove']"
        >删除</el-button>
      </el-col>-->
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['tongban:child:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="childList" @selection-change="handleSelectionChange">
<!--      <el-table-column type="selection" width="55" align="center" />-->
<!--      <el-table-column label="孩子id" align="center" prop="childId" />-->
      <el-table-column label="机构名称" align="center" prop="institutionName" />
      <el-table-column label="年级名称" align="center" prop="gradeName" />
      <el-table-column label="班级名称" align="center" prop="className" />
      <el-table-column label="孩子名称" align="center" prop="childName" />
      <el-table-column label="孩子乳名" align="center" prop="childInfantname" />
      <el-table-column label="孩子性别" align="center" prop="childSex" :formatter="sexTypeFormat" />
      <el-table-column label="孩子生日" align="center" prop="childBirthday" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.childBirthday, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
<!--      <el-table-column label="孩子身份证号" align="center" prop="childIdcard" />
      <el-table-column label="孩子头像;对应文件表sys_file中的file_id" align="center" prop="childHead" />-->
      <el-table-column label="民族" align="center" prop="nation" />
      <el-table-column label="油田内/外" align="center" prop="isOilField" :formatter="isOilFieldFormat" />
      <el-table-column label="第一联系人名称" align="center" prop="firscontactsName" />
      <el-table-column label="第一联系人电话" align="center" prop="firscontacts" />
<!--      <el-table-column label="第二联系人电话" align="center" prop="secondContacts" />
      <el-table-column label="第二联系人名称" align="center" prop="secondContactsName" />-->
<!--      <el-table-column label="入园时间" align="center" prop="joinTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.joinTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>-->
      <el-table-column label="创建时间" align="center" prop="createTime" />
<!--      <el-table-column label="孩子状态:对应字典表sys_dictionary中的Status类型" align="center" prop="status" />
      <el-table-column label="考勤机是否拉去:对应字典表sys_dictionary中的IsNot类型" align="center" prop="isUpdate" />
      <el-table-column label="记录状态:对应字典表sys_dictionary中的RecordStatus类型" align="center" prop="recordStatus" />
      <el-table-column label="记录状态:对应字典表sys_dictionary中的RecordStatus类型" align="center" prop="createUser" />
      <el-table-column label="记录状态:对应字典表sys_dictionary中的RecordStatus类型" align="center" prop="updateUser" />-->
<!--      <el-table-column label="二维码" align="center" prop="guid" />-->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['tongban:child:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['tongban:child:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改孩子对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="孩子名称" prop="childName">
          <el-input v-model="form.childName" placeholder="请输入孩子名称" />
        </el-form-item>
        <el-form-item label="孩子乳名" prop="childInfantname">
          <el-input v-model="form.childInfantname" placeholder="请输入孩子乳名" />
        </el-form-item>
        <el-form-item label="孩子性别" prop="childSex">
          <el-select v-model="form.childSex" placeholder="请选择孩子性别">
            <el-option
              v-for="dict in sexTypeOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="parseInt(dict.dictValue)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="孩子生日" prop="childBirthday">
          <el-date-picker clearable size="small"
            v-model="form.childBirthday"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择孩子生日">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="孩子身份证号" prop="childIdcard">
          <el-input v-model="form.childIdcard" placeholder="请输入孩子身份证号" />
        </el-form-item>
<!--        <el-form-item label="孩子头像;对应文件表sys_file中的file_id" prop="childHead">
          <el-input v-model="form.childHead" placeholder="请输入孩子头像;对应文件表sys_file中的file_id" />
        </el-form-item>-->
        <el-form-item label="民族" prop="nation">
          <el-input v-model="form.nation" placeholder="请输入民族" />
        </el-form-item>
        <el-form-item label="油田类型" prop="isOilField">
          <el-select v-model="form.isOilField" placeholder="请选择油田类型">
            <el-option
              v-for="dict in isOilFieldOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="parseInt(dict.dictValue)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="联系人名称" prop="firscontactsName">
          <el-input v-model="form.firscontactsName" placeholder="请输入联系人名称" />
        </el-form-item>
        <el-form-item label="联系人电话" prop="firscontacts">
          <el-input v-model="form.firscontacts" placeholder="请输入联系人电话" />
        </el-form-item>

<!--        <el-form-item label="第二联系人电话" prop="secondContacts">
          <el-input v-model="form.secondContacts" placeholder="请输入第二联系人电话" />
        </el-form-item>
        <el-form-item label="第二联系人名称" prop="secondContactsName">
          <el-input v-model="form.secondContactsName" placeholder="请输入第二联系人名称" />
        </el-form-item>-->
        <el-form-item label="入园时间" prop="joinTime" v-if="showjoinTime">
          <el-date-picker clearable size="small"
            v-model="form.joinTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择入园时间">
          </el-date-picker>
        </el-form-item>
<!--        <el-form-item label="孩子状态:对应字典表sys_dictionary中的Status类型">
          <el-radio-group v-model="form.status">
            <el-radio label="1">请选择字典生成</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="考勤机是否拉去:对应字典表sys_dictionary中的IsNot类型" prop="isUpdate">
          <el-input v-model="form.isUpdate" placeholder="请输入考勤机是否拉去:对应字典表sys_dictionary中的IsNot类型" />
        </el-form-item>
        <el-form-item label="记录状态:对应字典表sys_dictionary中的RecordStatus类型">
          <el-radio-group v-model="form.recordStatus">
            <el-radio label="1">请选择字典生成</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="记录状态:对应字典表sys_dictionary中的RecordStatus类型" prop="createUser">
          <el-input v-model="form.createUser" placeholder="请输入记录状态:对应字典表sys_dictionary中的RecordStatus类型" />
        </el-form-item>
        <el-form-item label="记录状态:对应字典表sys_dictionary中的RecordStatus类型" prop="updateUser">
          <el-input v-model="form.updateUser" placeholder="请输入记录状态:对应字典表sys_dictionary中的RecordStatus类型" />
        </el-form-item>-->
        <el-form-item label="机构名称" prop="institutionId" v-if="deptList">
          <el-select v-model="form.institutionId" placeholder="请选择机构" clearable size="small" @change="selectGrade">
            <el-option
              v-for="dept in deptList"
              :key="dept.institutionId"
              :label="dept.institutionName"
              :value="dept.institutionId"
            />
          </el-select>
        </el-form-item>

        <el-form-item label="年级名称" prop="gradeId" v-if="gradeList">
          <el-select v-model="form.gradeId" placeholder="请选择年级" clearable size="small" @change="selectClass">
            <el-option
              v-for="dept in gradeList"
              :key="dept.gradeId"
              :label="dept.gradeName"
              :value="dept.gradeId"
            />
          </el-select>
        </el-form-item>

        <el-form-item label="班级名称" prop="classId" v-if="classList">
          <el-select v-model="form.classId" placeholder="请选择班级" clearable size="small" >
            <el-option
              v-for="dept in classList"
              :key="dept.classId"
              :label="dept.className"
              :value="dept.classId"
            />
          </el-select>
        </el-form-item>
        <!--        <el-form-item label="机构" prop="institutionId" v-if="deptList">
          <el-select v-model="form.institutionId" placeholder="请选择机构" clearable size="small" @change="selectGrade">
            <el-option
              v-for="dept in deptList"
              :key="dept.institutionId"
              :label="dept.institutionName"
              :value="dept.institutionId"
            />
          </el-select>
        </el-form-item>

        <el-form-item label="年级" prop="gradeId" v-if="gradeList">
          <el-select v-model="form.gradeId" placeholder="请选择年级" clearable size="small" @change="selectClass">
            <el-option
              v-for="dept in gradeList"
              :key="dept.gradeId"
              :label="dept.gradeName"
              :value="dept.gradeId"
            />
          </el-select>
        </el-form-item>

        <el-form-item label="班级" prop="classId" v-if="classList">
          <el-select v-model="form.classId" placeholder="请选择班级" clearable size="small" >
            <el-option
              v-for="dept in classList"
              :key="dept.classId"
              :label="dept.className"
              :value="dept.classId"
            />
          </el-select>
        </el-form-item>-->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listChild, getChild, delChild, addChild, updateChild, exportChild } from "@/api/tongban/child";
import {listInstitutionData} from "@/api/tongban/institution";
import {getGrade, listInstitutionGrade} from "@/api/tongban/grade";
import {listInstitutionClass} from "@/api/tongban/class";

export default {
  name: "Child",
  components: {
  },
  data() {
    return {
      showjoinTime: true,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 孩子表格数据
      childList: [],
      // 弹出层标题
      deptList: [],
      gradeList: [],
      classList: [],
      title: "",
      // 是否显示弹出层
      open: false,
      // 孩子性别字典
      sexTypeOptions: [],
      // 是否是油田字典
      isOilFieldOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        institutionName: null,
        gradeName: null,
        className: null,
        childName: null,
        childInfantname: null,
        childSex: null,
        childBirthday: null,
        childIdcard: null,
        childHead: null,
        nation: null,
        isOilField: null,
        firscontacts: null,
        firscontactsName: null,
        secondContacts: null,
        secondContactsName: null,
        joinTime: null,
        status: null,
        isUpdate: null,
        recordStatus: null,
        createUser: null,
        updateUser: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        childName: [
          { required: true, message: "孩子名称不能为空", trigger: "blur" }
        ],
        childSex: [
          { required: true, message: "孩子性别不能为空", trigger: "change" }
        ],
        isOilField: [
          { required: true, message: "油田类型不能为空", trigger: "blur" }
        ],
        firscontactsName: [
          { required: true, message: "系人不能为空", trigger: "blur" }
        ],
        firscontacts: [
          { required: true, message: "系人手机号不能为空", trigger: "change" }
        ],
        /*
        createUser: [
          { required: true, message: "孩子状态:对应字典表的Status类型不能为空", trigger: "blur" }
        ],*/
      }
    };
  },
  created() {
    this.getList();
    this.getDicts("sex_type").then(response => {
      this.sexTypeOptions = response.data;
    });

    this.getDicts("is_oil_field").then(response => {
      this.isOilFieldOptions = response.data;
    });

    listInstitutionData().then(res => {
      this.deptList = res.data;
    });
  },
  methods: {
    /** 查询孩子列表 */
    getList() {
      this.loading = true;
      listChild(this.queryParams).then(response => {
        this.childList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 孩子性别字典翻译
    sexTypeFormat(row, column) {
      return this.selectDictLabel(this.sexTypeOptions, row.childSex);
    },

    //油田字典
    isOilFieldFormat(row, column) {
      return this.selectDictLabel(this.isOilFieldOptions, row.isOilField);
    },

    /** 查询年级*/
    selectGrade(id){
      listInstitutionGrade(id).then(res =>{
        this.gradeList = res.data;
      })
    },

    /** 查询班级*/
    selectClass(id){
      listInstitutionClass(id).then(response => {
        this.classList = response.data;
      })
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        childId: null,
        childName: null,
        childInfantname: null,
        childSex: null,
        childBirthday: null,
        childIdcard: null,
        childHead: null,
        nation: null,
        isOilField: null,
        firscontacts: null,
        firscontactsName: null,
        secondContacts: null,
        secondContactsName: null,
        joinTime: null,
        status: 0,
        isUpdate: null,
        recordStatus: 0,
        createTime: null,
        createUser: null,
        updateTime: null,
        updateUser: null,
        guid: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.childId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.showjoinTime = true;
      this.reset();
      this.open = true;
      this.title = "添加孩子";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.showjoinTime = false;
      this.reset();
      const childId = row.childId || this.ids
      getChild(childId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改年级";
        console.log(response.data);
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.childId != null) {
            updateChild(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addChild(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const childIds = row.childId || this.ids;
      this.$confirm('是否确认删除孩子编号为"' + childIds + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delChild(childIds);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有孩子数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportChild(queryParams);
        }).then(response => {
          this.download(response.msg);
        })
    }
  }
};
</script>
